package com.vhuan.self_discipline_service.managerimpl.wxmini;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.vhuan.common.constant.api.wxmini.WxminiGrantType;
import com.vhuan.common.domain.api.wxmini.WxminiLoginResponse;
import com.vhuan.self_discipline_service.manager.wxmini.WxminiManager;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
@RequiredArgsConstructor
public class WxminiManagerImpl implements WxminiManager {
    @Value("${wxmini.appid}")
    private String APPID;
    @Value("${wxmini.secret}")
    private String APP_SECRET;
    @Value("${wxmini.api-url}")
    private  String WXMINI_API_URL;
    @Override
    public WxminiLoginResponse login(String code) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("appid", APPID);
        params.put("secret", APP_SECRET);
        params.put("js_code", code);
        params.put("grant_type", WxminiGrantType.AUTHORIZATION_CODE);

        String result= HttpUtil.get(WXMINI_API_URL + WxminiGrantType.AUTHORIZATION_CODE_URL, params);

        return JSONUtil.toBean(result, WxminiLoginResponse.class);
    }
}
